package com.zealfi.wk.stream.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.core.MessageSource;
import org.springframework.messaging.support.GenericMessage;

import java.util.Date;

/**
 * Created by kuan at 2018-05-14 下午 5:41
 */
@EnableBinding(AppProcessor.class)
public class App2 {
    private static final Logger logger = LoggerFactory.getLogger(App2.class);

    @Bean
    @InboundChannelAdapter(value = AppProcessor.OUTPUT, poller = @Poller(fixedDelay = "2000"))
    public MessageSource<Date> timerMessageSource() {
        return () -> {
            GenericMessage<Date> message = new GenericMessage<>(new Date());
            logger.info("生成message:" + message.getPayload().toInstant().toString());

            return message;
        };
    }

    @StreamListener(AppProcessor.INPUT)
    public void receiveFromOutput(Object payload) {
        logger.info("App2 Received: " + payload);
    }
}
